package com.jf.car.dao.impl;

import com.jf.car.bean.Car;
import com.jf.car.dao.CarDao;
import com.jf.car.util.BaseDao;
import com.jf.car.util.MyUtils;
import com.jf.car.util.PageInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CarDaoImpl implements CarDao {
    @Override
    public List<Car> getPage(PageInfo pageInfo, Car car) {
        String sql = "select car.*,cg.name from car left join category cg on car.category_id = cg.id  where 1 = 1 ";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(car.getCarNo())){
            sql+=" and car.carNo = ?";
            objs.add(car.getCarNo());
        }
        if(MyUtils.isStringNotEmpty(car.getColor())){
            sql+=" and car.color = ? ";
            objs.add(car.getColor());
        }
        if(car.getCategoryId() != null){
            sql+=" and car.category_id = ?";
            objs.add(car.getCategoryId());
        }
        if(car.getStatus() != null){
            sql+=" and car.status = ? ";
            objs.add(car.getStatus());
        }
        sql+="order by car.id desc limit ?,?";
        objs.add(pageInfo.getOffset());
        objs.add(pageInfo.getPageSize());
        return BaseDao.baseQuery(Car.class,sql,objs);
    }

    @Override
    public int getCount(Car car) {
        String sql = "select count(car.id) from car left join category cg on car.category_id = cg.id  where 1 = 1 ";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(car.getCarNo())){
            sql+=" and car.carNo = ?";
            objs.add(car.getCarNo());
        }
        if(MyUtils.isStringNotEmpty(car.getColor())){
            sql+=" and car.color = ? ";
            objs.add(car.getColor());
        }
        if(car.getCategoryId() != null){
            sql+=" and car.category_id = ?";
            objs.add(car.getCategoryId());
        }
        if(car.getStatus() != null){
            sql+=" and car.status = ? ";
            objs.add(car.getStatus());
        }
        return BaseDao.baseQueryCount(sql,objs);
    }

    @Override
    public Car getById(int id) {
        String sql = "select * from car where id = ? ";
        return BaseDao.baseQueryBean(Car.class,sql,id);
    }

    @Override
    public int addSelect(Car car) {
        String sql = "insert into car(";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(car.getCarNo())){
            sql+=" carNo,";
        }
        if(MyUtils.isStringNotEmpty(car.getColor())){
            sql+="color,";
        }
        if(car.getCategoryId() != null){
            sql+="category_id,";
        }
        if(car.getStatus() != null){
            sql+="status,";
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=") values(";
        if(MyUtils.isStringNotEmpty(car.getCarNo())){
            sql+=" carNo,";
        }
        if(MyUtils.isStringNotEmpty(car.getColor())){
            sql+="color,";
        }
        if(car.getCategoryId() != null){
            sql+="category_id,";
        }
        if(car.getStatus() != null){
            sql+="status,";
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=")";
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int updateSelect(Car car) {
        String sql = "update car set(";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(car.getCarNo())){
            sql+=" carNo = ?,";
            objs.add(car.getCarNo());
        }
        if(MyUtils.isStringNotEmpty(car.getColor())){
            sql+=" color = ?,";
            objs.add(car.getColor());
        }
        if(car.getCategoryId() != null){
            sql+="category_id = ?,";
            objs.add(car.getCarNoImage());
        }
        if(car.getStatus() != null){
            sql+="status = ?,";
            objs.add(car.getStatus());
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+="where id = ?";
        objs.add(car.getId());
        return BaseDao.baseUpdate(sql,objs);
    }


    @Override
    public int delete(int id) {
        String sql = "delete * from car where id = ?";
        return BaseDao.baseUpdate(sql,id);
    }

    @Override
    public Car getByCarNo(String carNo) {
        String sql = "select * from car where carNo = ?";
        return BaseDao.baseQueryBean(Car.class,sql,carNo);
    }

    @Override
    public List<Car> getCarByCategoryId(int categoryId) {
        String sql ="select * from car where category_id = ?";
        return BaseDao.baseQuery(Car.class,sql,categoryId);
    }

    @Override
    public List<Car> getList() {
        String sql = "select * from car";
        return BaseDao.baseQuery(Car.class,sql);
    }
}
